package main

import (
	"flag"
	"fmt"
	"gitee.com/wayjin/fabric-explorer/pkg"
	"github.com/gin-gonic/gin"
	"log"
)

var testConfig = "./test/config.json"

var (
	configFile string
	port int
)

func main() {
	flag.StringVar(&configFile, "config", "", "expected config file")
	flag.IntVar(&port, "port", 9872, "expected service port")
	flag.Parse()

	router := gin.Default()

	svc, err := pkg.NewService(testConfig)
	if err != nil {
		log.Fatal(err)
	}

	routerV1 := router.Group("v1")
	{
		routerV1.POST("/user/register", svc.RegisterUser)
		routerV1.POST("/user/login", nil)
		routerV1.GET("/channel/list", svc.ListChannels)
		routerV1.GET("/channel/list/:channelid/blocks/:count", svc.ListBlocks)
		routerV1.GET("/channel/list/:channelid/chaincodes", svc.ListChainCode)
		routerV1.GET("/channel/fetch/:channelid/blockchain", svc.GetBlockChain)
		routerV1.GET("/channel/fetch/:channelid/block/:number", svc.GetBlock)
		routerV1.POST("/channel/:channelid/chaincode/query/:name/:version", svc.QueryChainCode)
		routerV1.POST("/channel/:channelid/chaincode/invoke/:name/:version", svc.InvokeChainCode)
	}

	err = router.Run(fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal(err)
	}
}